<template>
	<view class="discussPage">
		<u-navbar title="写帖子" :safeAreaInsetTop="true" titleStyle="color:#333333;fontSize:30rpx;fontWeight:bold">
			<view slot="left" class="navbarLeft" @click="cancelPost">取消</view>
			<!-- <view slot="right" class="navBarRight" @click="republish">发布</view> -->
		</u-navbar>
		<u-status-bar></u-status-bar>
		<view class="postContent">
			<view class="postContentheader">
				<textarea :maxlength="30" v-model="title"  placeholder="请输入标题（2-30个字符）" border='none' clearable
				placeholderStyle="color: #666666;fontSize: 48rpx" @input="monitorTitle" style="width: 100%;font-size: 48rpx"></textarea>
				<view class="topic" v-if="topicName">
					#{{topicName}}
				</view>
				<view class="cont">
					<view class="topic-title"  @click="selectTopic">
						#话题
					</view>
					<view class="num">
						{{inputNum}}/30
					</view>
				</view>
			</view>
			<view class="infoContent">
				<textarea v-model="info"  placeholder="来吧，想和大家分享点什么..."  count maxlength="4000" border="none"
					:adjustPosition="false" :show-confirm-bar="false" @blur="leaveInput" @focus="getHeight"
					:auto-height="true" placeholder-class="placeClass" placeholderStyle="color: #999999;" style="width: 100%;">
				</textarea>
			</view>
			<view v-if="showShareInfo" class="shareInfo">
				<view class="itemTop">
					<view class="topRight">
						<u--image :src="shareInfo.images?(api+'/sys/common/static/'+shareInfo.images.split(',')[0]):''"
							shape="square" mode="aspectFill" width="208rpx" height="150rpx" bgColor="transparent"
							radius="8rpx"></u--image>
					</view>
					<view class="topLeft">
						<view class="title">{{shareInfo.name}}</view>
						<view class="listInfo">{{shareInfo.describeDesc||shareInfo.content}}</view>
					</view>
				</view>
			</view>
			<view class="uploadContent">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					width="205rpx"
					height="205rpx"
					:maxCount="9">
					<view class="uploadIcon">
						<u-icon name="plus" size="50rpx" color="#D0D0D0FF"></u-icon>
					</view>
				</u-upload>
			</view>
			<view class="discussBtn" :style="[{ 'bottom':btnContentHeight -3 + 'px'}]">
				<view class="title">
					板块选择
				</view>
				<view class="plate-pannel">
					<view class="plate-item" :class="{'active': selectedModel==item.id}" v-for="(item,index) in modelList" :key="index" @click="selectModel(item)">
						{{item.columnName}}
					</view>
				</view>
			</view>
		</view>
		<view class="submit">
			<view class="submit-button" @click="republish">
				发布
			</view>
		</view>
	</view>
</template>

<script>
	import Tabbar from "@/components/tabbar.vue";
	export default {
		components: {
			Tabbar
		},
		data() {
			return {
				api: this.$api.baseUrl,
				showModeList: false,
				title: "",
				info: "",
				fileList1: [],
				selectedModel: "",
				selectedModelTitle: "模块",
				topicId: "",
				topicName: "",
				modelList: [],
				showShareInfo:false,
				userId: "",
				btnContentHeight: 0,
				shareData: {},
				shareInfo: {},
				inputNum: 0
				// currentChoose: ""
			}
		},
		onLoad(option) {
			if (!this.userId) {
				uni.getStorage({
					key: "userInfo",
					success: (res) => {
						console.log(res)
						let info = res.data;
						this.userId = info.id;
					}
				})
			}
			this.$request.get(this.$api.modelList).then(res => {
				this.modelList = res.result;
				uni.hideLoading()
			})
		},
		onShow() {
			if (!this.userId) {
				uni.getStorage({
					key: "userInfo",
					success: (res) => {
						let info = res.data;
						this.userId = info.id;
						this.shareData = uni.getStorageSync("shareData");
						uni.removeStorage({
							key: "shareData"
						})
						if (this.shareData.id) {
							this.initData();
						}
					}
				})
				this.$request.get(this.$api.modelList).then(res => {
					this.modelList = res.result;
					uni.hideLoading()
				})
			} else if (!this.shareData.id) {
				this.shareData = uni.getStorageSync("shareData");
				
				if (this.shareData.id) {
					this.initData();
				}
			}else{
				this.shareData = uni.getStorageSync("shareData");
				if (this.shareData.id) {
					this.initData();
				}
			}
			this.btnContentHeight = 0;
		},
		methods: {
			monitorTitle(e){
				let that = this
				that.inputNum = e.detail.value.length
			},
			leaveInput(e) {
				this.btnContentHeight = 0;
			},
			getHeight(e) {
				if (e) {
					this.btnContentHeight = e.target.height;
				}
			},
			initData() {
				if (this.shareData.types === "1") {
					this.$request.get(this.$api.postDetail, {
						userId: this.userId,
						id: this.shareData.id
					}).then(res => {
						uni.hideLoading()
						this.shareInfo = res.result;
						this.showShareInfo=true;
					})
				} else {
					this.$request.get(this.$api.getNewsDetail2, {
						id: this.shareData.id,
						userId: this.userId
					}).then(res => {
						uni.hideLoading()
						this.showShareInfo=true;
						this.shareInfo = res.result;
					})
				}
			},
			goPostsDetail() {
				uni.navigateTo({
					url: "/pages/postDetail/postDetail?id=" + this.shareData.id
				})
			},
			republish() {
				if (!this.title) {
					uni.showToast({
						icon: "none",
						title: "请输入标题"
					})
					return;
				}
				if (this.title.length<2) {
					uni.showToast({
						icon: "none",
						title: "请输入标题（2-30个字符）"
					})
					return;
				}
				if (!this.info) {
					uni.showToast({
						icon: "none",
						title: "请输入内容"
					})
					return;
				}
				if (!this.selectedModel) {
					uni.showToast({
						icon: "none",
						title: "请选择模块"
					})
					return;
				}
				let imageStr = "";
				this.fileList1.map(item => {
					if (imageStr) {
						imageStr = imageStr + "," + item.url.replaceAll(this.api + '/', "")
					} else {
						imageStr = item.url.replaceAll(this.api + '/', "")
					}
				})
				this.$request.post(this.$api.republishPost, {
					userId: this.userId,
					columnId: this.selectedModel,
					columnName: this.selectedModelTitle,
					topicId: this.topicId,
					//topicName: this.topicName,
					images: imageStr,
					content: this.info,
					name: this.title,
					postsParentId: this.shareData.id || "",
					types: this.shareData.types || ""
				}).then(res => {
					uni.hideLoading()
					this.cancelPost();
					this.title = ""
					this.info = ""
					this.fileList1 = [];
					this.selectedModel = ""
					this.selectedModelTitle = "模块"
					this.topicId = ""
					this.topicName = ""
					setTimeout(() => {
						uni.showToast({
							icon: "success",
							title: "发帖成功"
						})
					})
				})
			},
			selectModel(item) {
				this.selectedModel = item.id
				this.selectedModelTitle = item.columnName
				// this.showModeList = false;
			},
			cancelPost() {
				// if (this.shareData.id) {
				// 	if(this.shareData.types==='1'){
				// 		uni.redirectTo({
				// 			url: "/pages/postDetail/postDetail?id=" + this.shareData.id
				// 		})
				// 	}else if(this.shareData.types==='2'){
				// 		uni.redirectTo({
				// 			url: "/pages/newsDetial/newsDetial?id=" + this.shareData.id
				// 		})
				// 	}else if(this.shareData.types==='3'){
				// 		uni.redirectTo({
				// 			url: "/pages/solutionDetial/solutionDetial?id=" + this.shareData.id
				// 		})
				// 	}
				// } else {
				uni.removeStorage({
					key: "shareData"
				})
				this.showShareInfo=false;
				uni.switchTab({
					url: "/pages/community/community"
				})
				// }
			},
			async afterRead(event) {
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: this.api + '/' + result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					this.$request.upload(this.$api.uploadImage, url).then(res => {
						resolve(JSON.parse(res).message)
						uni.hideLoading()
					});
				})
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			selectTopic() {
				uni.navigateTo({
					url: "/pages/topic/topic"
				})
			},
			doClose() {
				this.showModeList = false
			}
		}
	}
</script>

<style lang="scss">
	.discussPage {
		background-color: #fff;
		height: 100vh;
		overflow: auto;
		.navbarLeft {
			color: #666666;
			font-size: 30rpx;
		}

		.navBarRight {
			color: #264DA1;
			font-size: 30rpx;
		}

		.postContent {
			padding: 100rpx 40rpx 0;
			padding-bottom: 200rpx;
			.postContentheader {
				display: flex;
				flex-direction: column;
				border-bottom: 1rpx solid #D0D0D0;
				padding-bottom: 16rpx;
				.topic {
					color: #264DA1;
					font-size: 26rpx;
				}
				.cont {
					display: flex;
					flex-direction: row;
					justify-content: flex-end;
					.topic-title {
						color: #264DA1;
						font-size: 26rpx;
						margin-right: 16rpx;
					}
					.num {
						color: #999999;
						font-size: 26rpx;
					}
				}
			}
			.u-input {
				height: 50rpx;
				line-height: 60rpx;
				font-size: 46rpx;

				.u-input__content__field-wrapper__field {
					height: 50rpx;
					line-height: 60rpx;
					font-size: 46rpx !important;
				}
			}

			.infoContent {
				padding-top: 20rpx;

				.placeClass {
					font-size: 32rpx;
					color: #D0D0D0;
				}

				textarea {
					// border-bottom: 1rpx solid #D0D0D0;
					width: calc(100% - 40rpx);
					padding: 20rpx 20rpx;
					min-height: 300rpx;
				}
			}

			.shareInfo {
				display: flex;
				flex-direction: column;
				background: #FFFFFF;
				box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
				margin-bottom: 20rpx;
				position: relative;
				top: -2rpx;
				background-color: #F8F8F8;

				.itemTop {
					display: flex;
					flex-wrap: nowrap;
					justify-content: flex-start;

					.topLeft {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: 100%;
						padding-left: 20rpx;
						padding-bottom: 10rpx;

						.listInfo {
							color: #999999;
							font-size: 26rpx;
							line-height: 35rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 3;
							-webkit-box-orient: vertical;
							letter-spacing: 2rpx;
							height: 100rpx;
							font-family: PingFang SC-Regular, PingFang SC;
						}

						.title {
							padding-top: 10rpx;
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							font-size: 28rpx;
							letter-spacing: 1rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							-webkit-box-orient: vertical;
							padding-right: 20rpx;
						}

						.talk {
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #004DA1;
							font-size: 26rpx;
							display: flex;
							flex-wrap: nowrap;
							overflow: auto;
							margin-right: 20rpx;

							.talkItem {
								flex-shrink: 0;
							}
						}
					}

					.topRight {
						flex-shrink: 0;
					}
				}
			}

			.uploadContent {
				background-color: #fff;

				.u-upload__wrap {
					display: flex;
					justify-content: flex-start;
					margin-top: 20rpx;
				}

				.uploadIcon {
					width: 205rpx;
					height: 205rpx;
					background-color: #fff;
					font-size: 50rpx;
					margin: 20rpx 10rpx 0 0;
					border: 1px dashed #D0D0D0FF;
					display: flex;
					justify-content: center;
				}

				.u-upload__wrap__preview {
					margin-top: 20rpx;
				}
			}
		}

		.discussBtn {
			display: flex;
			flex-direction: column;
			// justify-content: space-between;
			border-top: 1rpx solid #D0D0D0;
			padding: 38rpx 0 0 0;
			margin-top: 40rpx;
			// margin: 0 30rpx;
			// position: fixed;
			// width: calc(100% - 80rpx);
			// height: 100rpx;
			background-color: #fff;
			z-index: 9;
			.title {
				color: #333333;
				font-size: 36rpx;
			}
			.plate-pannel {
				display: flex;
				flex-wrap: wrap;
				.plate-item {
					// min-width: 128rpx;
					height: 64rpx;
					line-height: 64rpx;
					text-align: center;
					background: #F4F4F4;
					border-radius: 8rpx;
					opacity: 1;
					margin-right: 16rpx;
					font-size: 26rpx;
					color: #333333;
					padding: 0 24rpx;
					margin-top: 28rpx;
					&.active {
						background: #E5EDFF;
						// border-radius: 8px 8px 8px 8px;
						opacity: 1;
						color: #004DA1;
					}
				}
			}
			.modelSelect {
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				align-items: center;
				border: 1px solid #F0F0F0FF;
				height: 54rpx;
				border-radius: 27rpx;
				padding: 0 20rpx;
				flex-shrink: 0;
				.title {
					padding-left: 10rpx;
					font-size: 24rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
				}
			}

			.topicText {
				color: #004DA1FF;
				font-size: 26rpx;
			}
			.navBarRight{
				flex-shrink: 0;
			}
		}

		.midelPanel {
			display: flex;
			text-align: center;
			flex-direction: column;

			.title {
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
				font-size: 28rpx;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;
			}

			.cost {
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				display: flex;
				flex-wrap: nowrap;
				justify-content: center;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;
			}

			.modelBtn {
				display: flex;
				justify-content: center;
				padding-top: 20rpx;

				.btn {
					height: 92rpx;
					line-height: 92rpx;
					width: 680rpx;
					font-size: 32rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
					text-align: center;
					background: #F6F6F6;
					border-radius: 48rpx;
				}
			}

		}
		.submit {
			position: fixed;
			padding: 36rpx;
			padding-bottom: 66rpx;
			background: #FFFFFF;
			bottom: 0;
			left: 0;
			width: 100%;
			box-sizing: border-box;
			background: #FFFFFF;
			z-index: 999;
			.submit-button {
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
				border-radius: 48rpx 48rpx 48rpx 48rpx;
				opacity: 1;
				color: #FFFFFF;
				font-size: 36rpx;
			}
		}
	}
</style>
